/* JavaStreamWrapper.cpp
 *
 * --
 * Copyright (c) 2012, Christophe Huriaux
 * Distributed under the terms of the MIT license.
 * http://www.opensource.org/licenses/mit-license.php
 * --
 */

#include <wrapper/JavaStreamWrapper.h>
#include <debug/logger.h>
#include <cstring>

JavaStreamWrapper::JavaStreamWrapper(std::ostream& stream)
	: os(stream)
{
}

void JavaStreamWrapper::writeBoolean(bool v)
{
	if (v)
		os.put(0x01);
	else
		os.put(0x00);
}

void JavaStreamWrapper::writeByte(int v)
{
	os.put((char)v);
}

void JavaStreamWrapper::writeBytes(std::string& s)
{

}

void JavaStreamWrapper::writeChar(int c)
{

}

void JavaStreamWrapper::writeChars(std::string& s)
{

}

void JavaStreamWrapper::writeDouble(double v)
{

}

void JavaStreamWrapper::writeFloat(float v)
{

}

void JavaStreamWrapper::writeInt(int v)
{
	char* ptr = (char*)&v;

	os.put(ptr[3]);
	os.put(ptr[2]);
	os.put(ptr[1]);
	os.put(ptr[0]);
}

void JavaStreamWrapper::writeLong(long v)
{
	char* ptr = (char*)&v;

	os.put(ptr[7]);
	os.put(ptr[6]);
	os.put(ptr[5]);
	os.put(ptr[4]);
	os.put(ptr[3]);
	os.put(ptr[2]);
	os.put(ptr[1]);
	os.put(ptr[0]);
}

void JavaStreamWrapper::writeShort(int v)
{
	char* ptr = (char*)&v;

	os.put(ptr[1]);
	os.put(ptr[0]);
}

void JavaStreamWrapper::writeUTF(std::string& str)
{
	const char *cstr = str.c_str();
	writeShort(str.length());

	for (unsigned int i = 0; i < str.length(); i++)
	{
		os.put(0x00);
		os.put(cstr[i]);
	}
}

bool JavaStreamWrapper::readBoolean(void *b)
{
	return JavaStreamWrapper::readUnsignedByte(b)?true:false;
}

int8_t JavaStreamWrapper::readByte(void *b)
{
	return ((int8_t*)b)[0];
}

double JavaStreamWrapper::readDouble(void *b)
{
	return .0;
}

float JavaStreamWrapper::readFloat(void *b)
{
	return .0;
}

int32_t JavaStreamWrapper::readInt(void *b)
{
	char buf[4];

	buf[3] = ((char*)b)[0];
	buf[2] = ((char*)b)[1];
	buf[1] = ((char*)b)[2];
	buf[0] = ((char*)b)[3];

	return *((int32_t*)buf);
}

int64_t JavaStreamWrapper::readLong(void *b)
{
	char buf[8];

	buf[7] = ((char*)b)[0];
	buf[6] = ((char*)b)[1];
	buf[5] = ((char*)b)[2];
	buf[4] = ((char*)b)[3];
	buf[3] = ((char*)b)[4];
	buf[2] = ((char*)b)[5];
	buf[1] = ((char*)b)[6];
	buf[0] = ((char*)b)[7];

	return *((int32_t*)buf);
}

int16_t JavaStreamWrapper::readShort(void *b)
{
	char buf[2];

	buf[1] = ((char*)b)[0];
	buf[0] = ((char*)b)[1];

	return *((int16_t*)buf);
}

uint8_t JavaStreamWrapper::readUnsignedByte(void *b)
{
	return ((uint8_t*)b)[0];
}

uint16_t JavaStreamWrapper::readUnsignedShort(void *b)
{
	char buf[2];

	buf[1] = ((char*)b)[0];
	buf[0] = ((char*)b)[1];

	return *((uint16_t*)buf);
}

std::string JavaStreamWrapper::readUTF(void *b, int16_t len)
{
	std::string ret(len, 0);

	for (int i = 0; i < len; i++)
		ret[i] = ((char*)b)[2*i+1];

	return ret;
}


void
JavaArrayWrapper<int8_t>::readArray(void *b, std::vector<int8_t>& out, ssize_t numElem)
{
	for (int i = 0; i < numElem; i++)
		out.push_back(((int8_t*)b)[i]);
}

void
JavaArrayWrapper<uint8_t>::readArray(void *b, std::vector<uint8_t>& out, ssize_t numElem)
{
	for (int i = 0; i < numElem; i++)
		out.push_back(((uint8_t*)b)[i]);
}
